# import copy

# class Solution:
#     def longestAwesome(self, s: str) -> int:
#         mapper={}
#         maxlen=0
#         oddcount=0
#         for c in s:
#             try:
#                 mapper[c]+=1
#             except KeyError:
#                 mapper[c]=1
#             if mapper[c]%2==1:
#                 oddcount+=1
#             else:
#                 oddcount-=1
#         if oddcount==len(s):
#             return 1
#         if oddcount<2:
#             return len(s)
#         for i in range(len(s)):
#             #print(mapper,oddcount)
#             tempcount=oddcount           
#             temp=copy.deepcopy(mapper)
#             if oddcount==1 and len(s)-i>maxlen:
#                 return len(s)-i
#             for j in range(len(s)-1,i-1+maxlen,-1):
#                 mapper[s[j]]-=1
#                 if mapper[s[j]]%2==1:
#                     oddcount+=1
#                 else:
#                     oddcount-=1
#                 if oddcount<2:
#                     if j-i>maxlen:
#                         print(mapper)
#                         maxlen=j-i
#                     break
#             mapper=temp
#             mapper[s[i]]-=1
#             oddcount=tempcount
#             if mapper[s[i]]%2==1:
#                 oddcount+=1
#             else:
#                 oddcount-=1 
#         return maxlen


class Solution:
    def longestAwesome(self, s: str) -> int:
        mapper={}
        prefix={0:-1}
        maxlen=0
        sequence=0
        for i in range(len(s)):
            digit=ord(s[i])-ord('0')
            sequence ^=(1 << digit)
            if sequence in prefix:
                maxlen=max(maxlen,i-prefix[sequence])
            else:
                prefix[sequence]=i
            for j in range(10):
                temp=sequence ^ (1<<j)
                if temp in prefix:
                    maxlen=max(maxlen,i-prefix[temp])
        return maxlen     
s=Solution()
print(s.longestAwesome("213123"))